package com.wenlin.myproduction.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import androidx.annotation.IntRange;
import androidx.annotation.Keep;
import androidx.annotation.Nullable;

import com.wenlin.myproduction.util.Checker;


/**
 * @author 文琳
 * @time 2020/4/21 16:35
 * @desc 带红色粉红相间的圆角进度条 改自git项目，原地址：https://github.com/RottenWang/TBProgressBar
 */
public class RoundBlueProgressBar extends View {
    private static final float BORDER_WIDTH = 5f;
    private Paint paint;
    private int backgroundDarkColor = Color.parseColor("#B8B8B8");//灰黑
    private int backgroundLightColor = Color.parseColor("#CECECE");//灰白
    private int foregroundDarkColor = Color.parseColor("#2CB5FC");//暗蓝
    private int foregroundLightColor = Color.parseColor("#2ECFFC");//亮蓝
    private LinearGradient mGroundLinearGradient;
    private LinearGradient mForegroundLinearGradient;
    private int[] backgroundColors;
    private int[] foregroundColors;
    private float[] positions;
    private int progress = 0;
    private PorterDuffXfermode mMode;//图形的交集显示模式，叠加、去重等等，此处用的是叠加
    private ObjectAnimator oa;
    private int oldProgress = 0;

    public RoundBlueProgressBar(Context context) {
        this(context, null);
    }

    public RoundBlueProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundBlueProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setFilterBitmap(true);
        backgroundColors = new int[]{backgroundLightColor, backgroundDarkColor};
        foregroundColors = new int[]{foregroundLightColor, foregroundDarkColor};
        positions = new float[]{0.5f, 0.5f};
//        setBackgroundColor(Color.GRAY);
        mMode = new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP);
    }

    /**
     * 设置进度 range 0~100
     *
     * @param progress 进度
     */
    @Keep
    public void setProgress(@IntRange(from = 0, to = 100) int progress) {
        oldProgress = this.progress;
        this.progress = progress;
        invalidate();
    }

    /**
     * 开始演示动画
     */
    public void startShowAnimate() {
        oa = ObjectAnimator.ofInt(this, "progress", oldProgress, this.progress);
        oa.setDuration(1000).setInterpolator(new AccelerateDecelerateInterpolator());
        oa.setRepeatMode(ObjectAnimator.RESTART);
        oa.setRepeatCount(0);
        oa.start();
    }

    /**
     * 停止演示动画
     */
    public void stopShowAnimate() {
        if (Checker.notNull(oa))
            oa.cancel();
    }

//    @Override
//    protected void onAttachedToWindow() {
//        super.onAttachedToWindow();
//        startShowAnimate();
//    }
//
//    @Override
//    protected void onDetachedFromWindow() {
//        super.onDetachedFromWindow();
//        stopShowAnimate();
//    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth() - getPaddingLeft() - getPaddingRight();
        int height = getHeight() - getPaddingBottom() - getPaddingTop();
        //画背景的棱条
        if (mGroundLinearGradient == null) {
            mGroundLinearGradient = new LinearGradient(0, 7f, 7f, 0, backgroundColors, positions, Shader.TileMode.REPEAT);
        }
        if (mForegroundLinearGradient == null) {
            mForegroundLinearGradient = new LinearGradient(0, 7f, 7f, 0, foregroundColors, positions, Shader.TileMode.REPEAT);
        }
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(0, 0, width, height, height / 2f, height / 2f, paint);
        //将渐变的笔触装入画笔中，这样画笔画出来的线条就带着这种渐变的笔触
        paint.setStyle(Paint.Style.FILL);
        paint.setShader(mGroundLinearGradient);
        //画出背景，并且勾出两侧的圆角
        int i = canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);
        canvas.translate(getPaddingLeft(), getPaddingTop());
        canvas.drawRoundRect(BORDER_WIDTH, BORDER_WIDTH, width - BORDER_WIDTH, height - BORDER_WIDTH, (height - 2 * BORDER_WIDTH) / 2f, (height - 2 * BORDER_WIDTH) / 2f, paint);
        //画前景的圆角
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(foregroundDarkColor);
        paint.setXfermode(mMode);
        paint.setShader(mForegroundLinearGradient);
        canvas.drawRoundRect(BORDER_WIDTH, BORDER_WIDTH, width * progress / 100f - BORDER_WIDTH, height - BORDER_WIDTH, (height - 2 * BORDER_WIDTH) / 2f, (height - 2 * BORDER_WIDTH) / 2f, paint);
        //重置原点，重置画布状态
        canvas.translate(-getPaddingLeft(), -getPaddingTop());
        canvas.restoreToCount(i);
        paint.setXfermode(null);
        paint.setShader(null);
    }
}
